import gameView from './view'
import gameModel from './model'

class GameController {
  constructor() {
    this.gameView = gameView
    this.gameModel = gameModel
    this.gameModel.stageChanged.attach((sender, args) => {
      const stageName = args.stage
      switch (stageName) {
        case 'game-over':
          this.gameView.showGameOverPage()
          break
        case 'game':
          this.gameView.showGamePage()
          break
        default:
      }
    })
  }

  /**
   * 绑定事件触发回调函数
   */
  initPages() {
    // 保证showGameOverPage this指针指向该class实例,

    // 通过callback 完成 gamepage  和 gameOverPage 页面间通信；
    const gamePageCallbacks = {
      showGameOverPage: () => {
        console.log("set stage game-over")
        this.gameModel.setStage('game-over')
      }
    }

    



    //  从直接渲染到通过model驱动view
    const gameOverPagesCallbacks = {
      gameRestart: () => {
        this.gameModel.setStage('game')
      }
    }
    this.gameView.initGamePage(gamePageCallbacks)
    this.gameView.initGameOverPage(gameOverPagesCallbacks)
    this.gameModel.setStage('game')
  }

} 

export default new GameController()